<?php

namespace Business\Controller;

use Business\Controller\BaseController;

class BusinessController extends BaseController {
    
    public function _initialize() {
        parent::_initialize();
        if (!$this->wid) {
            $this->error('请先登录');
        }
    }
    
    //获取上课状态
    public function workState(){
        $isonwork = M('seller')->where(['wid'=>$this->wid])->getField('isonwork');
        $this->returnList($isonwork);
    }
    
    //编辑商品
    public function editGoodsInfo(){
        $this->_authSign($this->wid);
		
        $data['lon'] = $this->getParam('lon');
        $data['lat'] = $this->getParam('lat');
        $data['intro'] = $this->getParam('intro');
        $data['nickname'] = $this->getParam('nickname');
        $data['headpic'] = $this->getParam('headpic');
        $data['age'] = $this->getParam('age');
        $data['height'] = $this->getParam('height');
        $data['weight'] = $this->getParam('weight');
        $data['bust'] = $this->getParam('bust');
        $data['price'] = $this->getParam('price');
        $data['xingzuo'] = $this->getParam('xingzuo');
        $data['old_address'] = $this->getParam('old_address');
        $data['address'] = $this->getParam('address');
        $data['detail_address'] = $this->getParam('detail_address');
        $data['ishome'] = $this->getParam('ishome');
        $data['arr_img'] = $_POST['arr_img'];
        $data['video_url'] = $this->getParam('video_url');
        $data['video_img'] = $this->getParam('video_img');
        $data['qq'] = $this->getParam('qq');
        $data['qqnum'] = $this->getParam('qqnum');
        $data['receivenum'] = $this->getParam('receivenum');
        $data['wx'] = $this->getParam('wx');
        $data['remark'] = $this->getParam('remark');
        $data['city'] = $this->getParam('city');
        $data['activityid'] = $this->getParam('activityid');
        $data['gender'] = $this->getParam('gender');
        $data['recommend_uid'] = $this->getParam('recommend_uid');
        $data['movie'] = $this->getParam('movie');
        $data['music'] = $this->getParam('music');
        $data['book'] = $this->getParam('book');
        $data['motion'] = $this->getParam('motion');
        $data['game'] = $this->getParam('game');
        $data['place'] = $this->getParam('place');
        $data['tags'] = $this->getParam('tags');
        $data['appearance'] = $this->getParam('appearance');
        $data['zodiac'] = $this->getParam('zodiac');
        $data['idol'] = $this->getParam('idol');
        $data['hobby'] = $this->getParam('hobby');
        $data['job'] = $this->getParam('job');
        $data['income'] = $this->getParam('income');
        $data['house'] = $this->getParam('house');
        $data['blood'] = $this->getParam('blood');
        
//        $arr_img_path = [];
//        foreach($arr_img as $val){
//            $arr_img_path[] = $this->uploadFileBase64($val,'Business');
//        }
//        $data['arr_img'] = json_encode($arr_img_path);
        $res = M('seller')->where(['wid'=>$this->wid])->save($data);
        $this->success();
    }
    
    //编辑商品名称
    public function editGoodsName(){
        $data['nickname'] = $this->getParam('nickname');
        $res = M('seller')->where(['wid'=>$this->wid])->save($data);
        $this->success();
    }
    
    //同意取消
    public function confirmCancel(){
        $order_id = $this->getParam('order_id',0,'int+',TRUE,'请选择订单');
        $where['order_id'] = $order_id;
        $where['wid'] = $this->wid;
        $res = M('order')->where($where)->save(['state'=>5]);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //设置密码
    public function setPaypsw(){
        $password = $this->getParam('password','','string',TRUE,'请输入密码');
        $res = M('seller')->where(['wid'=>$this->wid])->save(['pay_password'=>md5($password)]);
        $this->success('设置成功');
    }
    
    //修改用户昵称和头像
    public function updateUserinfo(){
        !empty($_POST['nickname']) && $data['nickname'] = $this->getParam('nickname');
//        !empty($_POST['headpic']) && $data['headpic'] = $this->uploadFileBase64($_POST['headpic'], 'Business');
        !empty($_POST['headpic']) && $data['headpic'] = $this->getParam('headpic');
        
        $res = M('seller')->where(['wid'=>$this->wid])->save($data);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //商家信息
    public function userInfo(){
        $info = M('seller')->where(['wid'=>$this->wid])->find();
        $this->returnList($info);
    }
    
    //商家信息
    public function sellerMiniInfo(){
        $info = M('seller')->field("wid,headpic,nickname,business_phone, pay_password")->where(['wid'=>$this->wid])->find();
        $this->returnList($info);
    }
    
    //发布动态
    public function publicDynamic(){
        $this->_authSign($this->wid);
        $where['wid'] = $this->wid;
        $where['create_time'] = array(array('EGT', date("Y-m-d 00:00:00")), array('ELT', date("Y-m-d H:i:s")));
        $pubCount = M('seller_content')->where($where)->count();
        if($pubCount > 5){
            $this->error('发布动态已到每天上限。');
        }
        
        
        $content = $this->getParam('content');
//        $arr_base64 = explode(',', $_POST['img_path']);
//        $arr_path = [];
//        foreach($arr_base64 as $val){
//            $arr_path[] = $this->uploadFileBase64($val,'Business');
//        }
//        $data['img_path'] = json_encode($arr_path);
        
        $data['img_path'] = $_POST['img_path'];
        $data['content'] = $content;
        $data['type'] = $this->getParam('type');
        $data['video_img'] = $this->getParam('video_img');
        $data['video_url'] = $this->getParam('video_url');
        $data['jump_type'] = $this->getParam('jump_type');
        $data['jump_value'] = $this->getParam('jump_value');
        $data['city'] = $this->getParam('city');
        $data['wid'] = $this->wid;
        $res = M('seller_content')->add($data);
        if($res){
            $this->success('发布成功');
        }else{
            $this->error();
        }
    }
    
    //删除动态
    public function deleteDynamic(){
        $id = $this->getParam('id',0,'int+',TRUE,'请选择一条动态');
        $res = M('seller_content')->where(['id'=>$id])->delete();
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //确认线下支付
    public function confirmOfflinePay(){
        $order_id = $this->getParam('order_id',0,'int+',TRUE,'请选择订单');
        $where['order_id'] = $order_id;
        $where['wid'] = $this->wid;
        $res = M('order')->where($where)->save(['state'=>3]);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //订单列表
    public function orderList(){
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $is_today_order = $this->getParam('is_today_order',0,'int+');
        $state = $this->getParam('state',0,'int+');
        $where['o.wid'] = $this->wid;
        if($state == 2){
            //线下支付
            $where['o.pay_type'] = 1;
        }elseif($state == 3){
            $where['o.pay_type'] = 2;
        }
        if($is_today_order){
            $today = date('Y-m-d 00:00:00');
            $where['o.create_time'] = array('EGT',$today);
        }
        $list = M('order')
                ->alias('o')
                ->field('o.*, u.nickname,u.headpic,u.user_phone')
                ->join('ot_user AS u ON u.user_id = o.user_id')
                ->where($where)
                ->order('o.order_id DESC')
                ->limit($size)
                ->page($page)
                ->select();
        $this->returnList($list);
    }
    
    //修改上下课
    public function changeWorkState(){
        $isonwork = $this->getParam('isonwork',0,'int+',TRUE);
        $res = M('seller')->where(['wid'=>$this->wid])->save(['isonwork'=>$isonwork]);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    
    
}
